package com.eight.homepage.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.TbZone;
import com.eight.homepage.dao.TbZoneDao;
import com.eight.homepage.service.TbZoneService;
import com.eight.homepage.vo.TbZoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/26 15:11
 */
@Service
public class TbZoneServiceImpl implements TbZoneService {


    @Autowired
    private TbZoneDao tbZoneDao;
    @Override
    public Page<TbZone> findPage(Integer currentSize, Integer pageSize, TbZoneVo tbZoneVo) {
        Page<TbZone> page = new Page<>(currentSize,pageSize);
        QueryWrapper<TbZone> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(tbZoneVo.getZoneName())){
            wrapper.like("zone_name",tbZoneVo.getZoneName());
        }
        if(tbZoneVo.getIsDisable()!= null){
            wrapper.eq("is_disable",tbZoneVo.getIsDisable());
        }
        Page<TbZone> page1 = tbZoneDao.selectPage(page, wrapper);
        return page1;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        TbZone tbZone = new TbZone();
        tbZone.setId(id);
        tbZone.setIsDisable(status);
        tbZone.setGmtModified(LocalDateTime.now());
        int i = tbZoneDao.updateById(tbZone);
        return i;
    }

    @Override
    public int delete(String id) {
        QueryWrapper<TbZone> wrapper = new QueryWrapper<>();
        if(id != null){
            wrapper.eq("id",id);
        }
        int i = tbZoneDao.delete(wrapper);
        return i;
    }

    @Override
    public int updateZone(TbZone tbZone) {
        QueryWrapper<TbZoneVo> wrapper = new QueryWrapper<>();
        return 0;
    }

    @Override
    public int insert(TbZone tbZone) {

        tbZone.setId(IdUtil.getSnowflakeNextIdStr()+"");
        tbZone.setGmtCreate(LocalDateTime.now());
        tbZone.setGmtModified(LocalDateTime.now());
        tbZone.setIsDeleted(0);
        tbZone.setIsDisable(0);

        int insert = tbZoneDao.insert(tbZone);
        return insert;

    }

}
